<template>
  <el-row class="register-now">
    <el-col :span="24"
            class="col-24-style">
      <p class="header-style">
        操作持续进行中!
      </p>
    </el-col>
  </el-row>
</template>

<script>
import { userPermissionApi } from "@/services/UserPermission";
import { LoginBindAccountApi } from '@/services/WorkBench';
export default {
  data () {
    return {
      companyInfo: '',
      unique_token: '',
    }
  },
  methods: {
    // 根据域名获取单位信息
    getCompanyByDomain () {
      let domain =
        location.host.indexOf("localhost") >= 0
          ? "dev_platform.ihibuilding.cn"
          : location.host;
      userPermissionApi
        .getCompanyByDomain({
          data: {
            company_domain: domain
          }
        })
        .then(
          res => {
            console.log(res, "get company domain");
            window.localStorage.setItem(
              "companyInfo",
              JSON.stringify(res.data)
            );
            this.companyInfo = res.data;
            this.containerStyle = {
              backgroundImage: `url(${this.companyInfo.image_picture})`
            }
            // 设置页面title为接口返回的数据
            let title = document.getElementsByTagName("title");
            title[0].innerText = this.companyInfo.platform_name || this.companyInfo.company_name;
            // 如果设置了公司logo，就将浏览器标签页的icon修改为logo
            if (this.companyInfo.company_logo) {
              let logo = document.querySelector(".tab-icon");
              logo.href = this.companyInfo.company_logo;
            }
          },
          err => {
            console.log(err);
          }
        );
    },
    // 登录成功后清除旧数据，写入新数据
    clearStorage (data) {
      this.$store.state.switchCompId = 0;
      window.localStorage.clear();
      this.$store.dispatch("resetActiveTabsList", []);
      window.sessionStorage.setItem("access_token", data.access_token);
      window.localStorage.setItem(
        "companyInfo",
        JSON.stringify(this.companyInfo)
      );
      window.localStorage.setItem("user_id", data.user_id);
      window.localStorage.setItem("phone", data.phone);
      window.localStorage.setItem("comp_id", data.comp_id);
      window.sessionStorage.setItem("access_token", data.access_token);
      window.localStorage.setItem("userInfo", JSON.stringify(data));
      this.getUserAuth(data.user_id);
    },
    loginNoUserNO () {
      userPermissionApi
        .noUserThirdBind({
          params: {
            unique_token: this.unique_token,
          }
        })
        .then(
          res => {
            if (res.code === 0) {
              this.getCompanyByDomain()// 根据域名获取单位信息
              this.clearStorage(res.data);
            }
          },
          err => {
            console.log(err);
          }
        );
    },
    // 获取用户权限
    getUserAuth (userId) {
      userPermissionApi
        .getUserAuth({
          data: {
            user_id: userId
          }
        })
        .then(
          res => {
            console.log(res, "get user auth");
            let userAuthList = res.data.list;
            if (userAuthList) {
              this.$store.dispatch("setUserAuth", res.data.list);
            };
            var hrefUrl = process.env.JUMP_TO_BIND_PAGE + "workbenchMyUnit"
            // window.location.href = hrefUrl;
            top.location.href = hrefUrl;// 在顶层页面打开新页面
            // this.$message.success("操作成功");
          },
          err => {
            console.log(err);
          }
        );
    },
    // 根据系统编号和授权码获取key
    getKey (val) {
      let authCode = process.env.AUTH_CODE;
      userPermissionApi
        .getKey({
          data: {
            system_no: "industry",
            auth_code: authCode
          }
        })
        .then(
          res => {
            console.log(res, "get key");
            let data = res.data;
            if (data.key) {
              window.sessionStorage.setItem("platformKey", data.key);
            };
            if (val == 'userNo') {
            } else if (val == 'noUserNo') {
              this.loginNoUserNO();
            }
          },
          err => {
            console.log(err);
          }
        );
    },
    getYesOrNoBind () {
      LoginBindAccountApi.getYesOrNoBind({
        params: {
          system_no: 'dm',
          type: 0,
          code: (window.location.href.split('=')[1]).split('&')[0]
        }
      }).then((res) => {
        this.unique_token = res.data.unique_token;
        if (res.code == 0) {
          this.getKey('noUserNo')
        } else if (res.code == 33199) {//code=33199表示未绑定，需据此跳转到登录绑定页面
          top.location.href = process.env.JUMP_TO_BIND_PAGE + 'login?status=bindAcount&unique_token=' + res.data.unique_token
        }
      }).catch((error) => {
        console.log(error)
      })
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.getYesOrNoBind();//获取code
    });
  },
  created () {
  }
}
</script>

<style lang="scss" scoped>
.col-24-style {
  width: 100%;
  height: 70vh;
}
.header-style {
  font-size: 22px;
  width: 100%;
  height: 100%;
  text-align: center;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
</style>